package org.wisdom.context;

import org.springframework.util.Assert;

/**
 * 上下文缓存助手
 *
 * @author 张智研
 * @date 2022/05/23 12:12
 **/

public class WisdomContextHolder {
    private static final ThreadLocal<WisdomContext> CONTEXT_THREAD_LOCAL = new ThreadLocal<>();

    private WisdomContextHolder() {}

    public static void clearContext() {
        CONTEXT_THREAD_LOCAL.remove();
    }

    public static WisdomContext getContext() {
        WisdomContext ctx = CONTEXT_THREAD_LOCAL.get();
        if (ctx == null) {
            ctx = new WisdomContext();
            CONTEXT_THREAD_LOCAL.set(ctx);
        }
        return ctx;
    }

    public static void setContext(WisdomContext context) {
        Assert.notNull(context, "不可以为null");
        CONTEXT_THREAD_LOCAL.set(context);
    }

}
